<script language="JavaScript">
<!--
function Menu(cible,Objet)
{
	eval(cible+".location='"+Objet.options[Objet.selectedIndex].value+"'");
}
//-->
</script>

<?php
$req_cat = $bdd->query('SELECT * FROM categories_recettes ORDER BY nom');
?>
<table><tr>
<td>
<select id="categorie" name="categorie" onChange="Menu('parent',this)">
	<optgroup label="Catégories">
	<?php
	while ($donnees_cat = $req_cat->fetch())
	{
		?>
		<option value="index.php?page=recettes&cat=<?php echo $donnees_cat['id']; ?>" 
		<?php 
		if (isset($_GET['cat'])) {
			if ($donnees_cat['id'] == $_GET['cat']) {echo "selected";} 
		}
		?>
		><?php echo $donnees_cat['nom']; ?></option> 
		<?php
	}
	?>
	</optgroup>
</select>
</td>
<td>
<?php
$req_cat = $bdd->query('SELECT * FROM recettes ORDER BY nom');
?>
<select id="recettes" name="recettes" onChange="Menu('parent',this)">
	<optgroup label="Recettes">
	<?php
	while ($donnees_cat = $req_cat->fetch())
	{
		?>
		<option value="index.php?page=recettes&id=<?php echo $donnees_cat['id']; ?>"><?php echo substr($donnees_cat['nom'], 0, 40); ?>..</option> 
		<?php
	}
	?>
	</optgroup>
</select>
</td><td>
<?php
if ((isset($_SESSION['identifiant'])) AND (!empty($_GET['id'])))
{
	?>
	<form method="post" action="index.php?page=admin_recettes">
		<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
		<input type="submit" value="Editer" name="editer" class="bouton" /><input type="submit" value="Nouvel article" name="nouveau" class="bouton" />
	</form>
	<?php
}
?>
</td></tr></table>
<hr/>
<?php

if (isset($_GET['id'])) 
{
	//echo $_GET['id'];
	
	$req = $bdd->prepare('SELECT * FROM recettes WHERE id=:id');
	$req->execute(array('id' => $_GET['id']));
	$donnees = $req->fetch();
	?>
	<img src="<?php echo $donnees['image']; ?>" width="200" class="galerie-droit"><h1><?php echo $donnees['nom']; ?></h1>
	<h2>Ingrédients</h2>
	<p><?php echo stripslashes($donnees['ingredients']); ?></p>
	<h2>Préparation</h2>
	<p><?php echo stripslashes($donnees['preparation']); ?></p>
	<hr/>
	<?php
	if (!empty($donnees['notes']))
	{
		?>
		<h2>Notes</h2>
		<p class="texte"><?php echo $donnees['notes']; ?></p>
		<hr />
		<?php
	}
	$req->closeCursor();
}
elseif (isset($_GET['cat'])) 
{
	//echo $_GET['cat'];
	$req_cat = $bdd->prepare('SELECT * FROM categories_recettes WHERE id=:id');
	$req_cat->execute(array('id' => $_GET['cat']));
	$donnees_cat = $req_cat->fetch();
	?><h1><?php echo $donnees_cat['nom']; ?></h1><?php
	$req = $bdd->prepare('SELECT * FROM recettes WHERE categorie=:categorie ORDER BY nom');
	$req->execute(array('categorie' => $_GET['cat']));
	?><table border="0" width="100%"><?php
	$c=1;
	$max=5;
	while ($donnees = $req->fetch())
	{
		if ($c==1) { echo '<tr>'; }
		?>
		<td width="20%"><img src="<?php echo $donnees['image']; ?>" height="100" width="100" class="galerie"><br />
		<a href="index.php?page=recettes&id=<?php echo $donnees['id']; ?>"><?php echo $donnees['nom']; ?></a></td>
		<?php
		if ($c == $max) { $c=1; echo '</tr>'; } else { $c++; }
	}
	?>
	</table>
	<hr/>
	<?php
	$req->closeCursor();
}
else
{
	?>
	<article>
		<div id="tableau-col">
			<?php include ('./modules/recettes_hasard.php'); ?>
		</div>
		<div id="tableau-col">
			<?php include ('./modules/recettes_derniere.php'); ?>
		</div>
		<hr/>
	</article>
	<article>
		<?php
		$req_cat = $bdd->query('SELECT * FROM categories_recettes ORDER BY nom');
		while ($donnees_cat = $req_cat->fetch())
		{
			?>
			<h1><?php echo stripslashes($donnees_cat['nom']); ?></h1>
			<p><?php echo stripslashes($donnees_cat['description']); ?></p>
			<?php
				$req = $bdd->prepare('SELECT * FROM recettes WHERE categorie=:categorie ORDER BY id DESC LIMIT 5');
				$req->execute(array('categorie'  => $donnees_cat['id']));
				?><table border="0"><tr><?php
				
				while ($donnees = $req->fetch())
				{
					
					?>
					<td width="20%"><img src="<?php echo $donnees['image']; ?>" height="100" width="100" class="galerie"><br />
					<a href="index.php?page=recettes&id=<?php echo $donnees['id']; ?>"><?php echo $donnees['nom']; ?></a></td>
					<?php
					
				}
				$req->closeCursor();
			?>
			</tr>
			</table>
			<p class="menu"><a href="index.php?page=recettes&cat=<?php echo $donnees_cat['id']; ?>">Voir toutes les recettes</a></p>
			<hr />
			<?php
		}
		$req_cat->closeCursor();
		?>
	</article>
	<?php
}
?>